import { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import type { ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import semver from "semver";
import type { MirrorWebsiteFile } from "@common/spider-types.ts";
import { getMirrorOptions, type MirrorNames } from "@/modules/dev-env/mirror/data.ts";

/**
 * Maven下载源
 */
export class MavenMirror extends BaseEnvMirror {
  constructor(name: MirrorNames) {
    super(getMirrorOptions(name, name === "apache" ? "/maven/maven-3/" : "/apache/maven/maven-3/"));
  }

  protected getDownloadList0(files: MirrorWebsiteFile[]): ToolInstallVersionItem[] {
    const versions = files.filter(it => it.type === "dir" && /^\d+\..*$/.test(it.name));
    versions.sort((a, b) => semver.compare(b.name, a.name));
    return versions.map(it => ({
      id: `maven-${it.name}`,
      name: `apache-maven-${it.name}-bin.zip`,
      version: it.name,
      url: `${this.url}${it.name}/binaries/apache-maven-${it.name}-bin.zip`,
      date: it.date,
      size: it.size,
    }));
  }
}
